package toolkit;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class Parse
{
	// German is the closest to Dutch.
	private static NumberFormat format = NumberFormat.getInstance(Locale.GERMAN);
	
	public static void setFormat(NumberFormat format)
	{
		Parse.format = format;
	}
	
	public static byte toByte(String input)
	{
		return Byte.parseByte(input.trim());
	}
	
	public static short toShort(String input)
	{
		return Short.parseShort(input.trim());
	}
	
	public static int toInt(String input)
	{
		return Integer.parseInt(input);
	}
	
	public static long toLong(String input)
	{
		return Long.parseLong(input.trim());
	}
	
	private static Number parseRealNumber(String input)
	{
		input = input.trim();
		
		ParsePosition parser = new ParsePosition(0);
		
		format.setGroupingUsed(false);
		
		Number real = format.parse(input, parser);
		
		if (real == null || parser.getIndex() != input.length())
		{
			throw new NumberFormatException();
		}
		
		return real;
	}
	
	public static float toFloat(String input)
	{
		return parseRealNumber(input).floatValue();
	}
	
	public static double toDouble(String input)
	{
		return parseRealNumber(input).doubleValue();
	}

	public static String toNonCamelCase(String input)
	{
		Pattern pattern = Pattern.compile("\\p{Lu}");
		
        Matcher matcher = pattern.matcher(input);
        
        StringBuffer buffer = new StringBuffer();
        
        while (matcher.find())
        {
        	matcher.appendReplacement(buffer, " " + matcher.group());
        }
        
        matcher.appendTail(buffer);
        
        return buffer.toString().trim();
	}
	
	public static String toString(int input)
	{
		return Integer.toString(input);
	}
	
	public static String toString(double input)
	{
		return format.format(input);
	}
	
	public static String toString(float input)
	{
		return toString((double) input);
	}

	public static Date toDate(String date)
	{
		Date parsed = null;
		
		do
		{
			if (date == null)
			{
				break;
			}
			
			String datePattern = "dd-MM-yyyy";
			
			SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
			
			date = date.trim();
			
			if (date.trim().length() == datePattern.length())
			{
				break;
			}
			
			try
			{
				parsed = dateFormat.parse(date);
			}
			catch (ParseException e)
			{
				break;
			}
		}
		while (false);
		
		return parsed;
	}
	
	public static String toDateString(GregorianCalendar input)
	{
		int[] items = {GregorianCalendar.DAY_OF_MONTH, GregorianCalendar.MONTH, GregorianCalendar.YEAR};
		
		String[] output = new String[3];
		
		for (int i = 0; i < 3; i++)
		{
			int value = input.get(items[i]);
			
			output[i] = (value <= 9
				? "0" + value
				: "" + value
			);
		}
		
		return StringUtils.join(output, "-");
	}
	
	public static String toDateTimeString(GregorianCalendar input)
	{
		int[] items = {GregorianCalendar.HOUR_OF_DAY, GregorianCalendar.MINUTE, GregorianCalendar.SECOND};
		
		String[] output = new String[3];
		
		for (int i = 0; i < 3; i++)
		{
			int value = input.get(items[i]);
			
			output[i] = (value <= 9
				? "0" + value
				: "" + value
			);
		}
		
		return toolkit.Parse.toDateString(input) + " " + StringUtils.join(output, ":");
	}
	
	public static String toFileProtocol(String address)
	{
		address = "file:///" + address.replaceAll("\\\\", "/");
		
		try
		{
			address = address.replaceAll(" ", "%20");
		}
		catch (Exception e) {}
		
		return address;
	}
}